package com.rongkang.interview.jvm.quote;

import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;

/**
 * @Description:
 * @author: 荣康
 * @date: 2021/12/16
 *
 * 弱引用  ： 不管内存够不够用，只要发生GC就会被当做垃圾收集
 */
public class WeakReferenceDemo {
    public static void main(String[] args) {
        Object o1 = new Object();
        ReferenceQueue<Object> referenceQueue = new ReferenceQueue<>();
        WeakReference<Object> objectWeakReference = new WeakReference<>(o1,referenceQueue);
        System.out.println("o1 = " + o1);
        System.out.println("objectWeakReference = " + objectWeakReference.get());
        System.out.println("referenceQueue = " + referenceQueue.poll());
        o1=null;
        System.gc();
        System.out.println("o1 = " + o1);
        System.out.println("objectWeakReference = " + objectWeakReference.get());
        System.out.println("referenceQueue = " + referenceQueue.poll());

    }
}
